var gulp = require('gulp'),
    fs = require('fs'),
    concat = require('gulp-concat'),
    rename = require('gulp-rename'),
    less = require('gulp-less'),
    livereload = require('gulp-livereload'),
    rev = require('gulp-rev');
revcollect = require('gulp-rev-collector'),
    minicss = require('gulp-minify-css'),
    uglify = require('gulp-uglify'),
    del = require('del'),
    gulpif = require('gulp-if'),
    connect = require('gulp-connect'),
    runSequence = require('run-sequence');

var path = {
    src: {
        less: './src/less/*.less',
        css: './src/css/**/*.css',
        js: './src/js/**/*.js',
        img: './src/img/**/*.*',
        vendor: '.src/vendor',
        html: './src/*.html'
    },
    dest: {
        css: './dist/css',
        js: './dist/js',
        img: './dist/img',
        vendor: './dist/vendor',
        html: './dist',
    },
    rev: {
        less: './src/rev/less',
        lessSrc: './src/rev/less/*.less',
        css: './src/rev/css',
        js: './src/rev/js',
        img: './src/rev/img',
        vendor: './src/rev/vendor',
    }
}
var condition = true;

// imgages 根据MD5 获取版本号
gulp.task('revImg', function() {
    return gulp.src(path.src.img)
        .pipe(rev())
        .pipe(gulp.dest(path.dest.img))
        .pipe(rev.manifest())
        .pipe(gulp.dest(path.rev.img));
});

// less里更新引入文件版本号
gulp.task('revLess', function() {
    return gulp.src(['src/rev/**/*.json', 'src/less/*.less'])
        .pipe(revcollect())
        .pipe(gulp.dest(path.rev.less));
});

// 编译less 根据MD5 获取版本号
gulp.task('styles', function() {
    return gulp.src(path.rev.lessSrc)
        .pipe(less())
        .pipe(concat('app.css'))
        // .pipe(gulpif(condition, rename({ suffix: '.min' })))
        .pipe(gulpif(condition, minicss()))
        .pipe(rev())
        .pipe(gulp.dest(path.dest.css))
        .pipe(rev.manifest())
        .pipe(gulp.dest(path.rev.css));
});

// js MD5 获取版本号
gulp.task('scripts', function() {
    return gulp.src(path.src.js)
        .pipe(concat('main.js'))
        // .pipe(gulpif(condition, rename({ suffix: '.min' })))
        .pipe(gulpif(condition, uglify()))
        .pipe(rev())
        .pipe(gulp.dest(path.dest.js))
        .pipe(rev.manifest())
        .pipe(gulp.dest(path.rev.js));
});

// 更新html引入文件
gulp.task('revHtml', function() {
    return gulp.src(['./src/rev/**/*.json', path.src.html])
        .pipe(revcollect())
        .pipe(gulp.dest(path.dest.html))
        .pipe(connect.reload());
});

//意外出错？清除缓存文件
gulp.task('clean', function() {
    del(['./dist', './src/rev', './src/css']);
});

//开发构建
gulp.task('dev', function(done) {
    condition = false;
    runSequence(
        ['revImg'], ['revLess'], ['styles', 'scripts'], ['revHtml'],
        done);
});

//正式构建
gulp.task('build', function(done) {
    runSequence(
        ['revImg'], ['revLess'], ['styles', 'scripts'], ['revHtml'],
        done);
});

gulp.task('connect', function() {
    connect.server({
        port: 8080,
        root: 'dist',
        livereload: true
    });
});

// 监视文件刷新
gulp.task('watch', ['connect'], function() {
    gulp.watch(['./src/**/*.*',], ['dev']);
});


gulp.task('default', ['dev','connect','watch']);
